package com.uuah.server.transport.provider.mina.codecs.client;

import java.nio.ByteBuffer;
import java.util.Set;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecException;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午02:08:04
 * @version 1.0.0
 */
public class UuahResponseMessageDecoder implements ProtocolEncoder {
	protected transient Logger logger = LoggerFactory.getLogger(getClass());

	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws ProtocolCodecException {
		if (logger.isDebugEnabled()) {
			logger.debug("encode");
		}
		String val = message.toString();
		if (val.length() > 1024) {
			throw new ProtocolCodecException(
					"Cannot encode too long (1024 byte) strings.");
		}

		if (val.lastIndexOf("\r\n") < val.length() - 2) {
			val += "\r\n";
		}

		ByteBuffer buf = ByteBuffer.allocate(val.length());
		for (int i = 0; i < val.length(); i++) {
			buf.put((byte) val.charAt(i));
		}

		buf.flip();
		out.write(buf);
	}

	public Set getMessageTypes() {
		return null;
	}

	public void dispose(IoSession session) throws Exception {
		// Nothing to dispose
	}

}
